package com.pearadmin.pro.modules.school.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;
import com.pearadmin.pro.modules.school.domain.SchCalendarYear;
import com.pearadmin.pro.modules.school.domain.School;
import com.pearadmin.pro.modules.school.domain.SchoolSend;
import com.pearadmin.pro.modules.school.dto.SchoolDTO;
import com.pearadmin.pro.modules.school.param.SchoolRequest;
import com.pearadmin.pro.modules.school.repository.SchoolRepository;
import com.pearadmin.pro.modules.school.service.SchCalendarYearService;
import com.pearadmin.pro.modules.school.service.SchoolSendService;
import com.pearadmin.pro.modules.school.service.SchoolService;
import com.pearadmin.pro.modules.sys.domain.BusinessFile;
import com.pearadmin.pro.modules.sys.service.BusinessFileService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学校信息Service业务层处理
 *
 * @author ChenJian
 * @date 2021-08-29
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolRepository, School> implements SchoolService {
    @Resource
    private SchoolRepository schoolRepository;
    @Resource
    private SchoolSendService schoolSendService;
    @Resource
    private SchCalendarYearService schCalendarYearService;
    @Resource
    private BusinessFileService businessFileService;

    /**
     * 查询学校信息
     *
     * @param request
     * @return 学校信息 分页集合
     */
    @Override
    public PageResponse<School> page(SchoolRequest request) {
        LambdaQueryWrapper<School> querySchoolSend = new LambdaQueryWrapper<School>();
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchoolCnName()),School::getSchoolCnName,request.getSchoolCnName());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchoolEnName()),School::getSchoolEnName,request.getSchoolEnName());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchCountry()),School::getSchCountry,request.getSchCountry());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchResponsiblePerson()),School::getSchResponsiblePerson,request.getSchResponsiblePerson());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchCategory()),School::getSchCategory,request.getSchCategory());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchSchoolSystem()),School::getSchSchoolSystem,request.getSchSchoolSystem());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchOnlineYesno()),School::getSchOnlineYesno,request.getSchOnlineYesno());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchCompetitorYesno()),School::getSchCompetitorYesno,request.getSchCompetitorYesno());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchFollowUpProgress()),School::getSchFollowUpProgress,request.getSchFollowUpProgress());
        querySchoolSend.eq(ObjectUtil.isNotEmpty(request.getSchIntendedProject()),School::getSchIntendedProject,request.getSchIntendedProject());
        return Pageable.of(request, (() -> schoolRepository.selectList(querySchoolSend)));
    }

    @Override
    public Boolean saveOrUpdate(SchoolDTO request) {
        School school = GeneralConvertUtils.conv(request, School.class);
        saveOrUpdate(school);
        if (!ObjectUtils.isEmpty(request.getSchoolSendList())) {
            List<SchoolSend> schoolSendList = GeneralConvertUtils.convert2List(request.getSchoolSendList(), SchoolSend.class);
            schoolSendList.stream().forEach(s -> {
                s.setSchId(school.getId());
            });
            schoolSendService.saveOrUpdateBatch(schoolSendList);
        }
        if (!ObjectUtils.isEmpty(request.getSchCalendarYearList())) {
            List<SchCalendarYear> schCalendarYearList = GeneralConvertUtils.convert2List(request.getSchCalendarYearList(), SchCalendarYear.class);
            schCalendarYearList.stream().forEach(s -> {
                s.setSchId(school.getId());
            });
            schCalendarYearService.saveOrUpdateBatch(schCalendarYearList);
        }
        if (!ObjectUtils.isEmpty(request.getFiles())) {
            //保存附件
            List<BusinessFile> businessFileList = GeneralConvertUtils.convert2List(request.getFiles(), BusinessFile.class);
            businessFileList.stream().forEach(s -> {
                s.setBusinessId(school.getId());
            });
            businessFileService.saveOrUpdateBatch(businessFileList);
        }
        return true;
    }


    /**
     * 查询学校信息
     *
     * @param id 学校信息ID
     * @return 学校信息
     */
    @Override
    public SchoolDTO selectSchoolById(Long id) {
        SchoolDTO request = GeneralConvertUtils.conv(schoolRepository.selectById(id), SchoolDTO.class);

        LambdaQueryWrapper<SchoolSend> querySchoolSend = new LambdaQueryWrapper<>();
        querySchoolSend.eq(SchoolSend::getSchId, id);
        request.setSchoolSendList(schoolSendService.list(querySchoolSend));


        LambdaQueryWrapper<SchCalendarYear> querySchCalendarYear = new LambdaQueryWrapper<>();
        querySchCalendarYear.eq(SchCalendarYear::getSchId, id);
        request.setSchCalendarYearList(schCalendarYearService.list(querySchCalendarYear));

        LambdaQueryWrapper<BusinessFile> queryBusinessFile = new LambdaQueryWrapper<>();
        queryBusinessFile.eq(BusinessFile::getBusinessId, id);
        request.setFiles(businessFileService.list(queryBusinessFile));

        return request;

    }

}
